package com.ztgf.pay.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.trz.netwk.api.system.TrdMessenger;
import com.trz.netwk.api.trd.*;
import com.trz.netwk.api.vo.QuyDaT9007;
import com.trz.netwk.api.vo.QuyDaT9008;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.BigDecimalUtils;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.pay.common.config.YmlConfig;
import com.ztgf.pay.mapper.PayFundDetailsMapper;
import com.ztgf.pay.mapper.PayUserMapper;
import com.ztgf.pay.models.bo.PayUserBankcardSaveBO;
import com.ztgf.pay.models.bo.PayUserSaveBO;
import com.ztgf.pay.models.bo.PayZjBalanceBO;
import com.ztgf.pay.models.bo.PayZjBalanceLogBO;
import com.ztgf.pay.models.dto.PayFundDetailsDTO;
import com.ztgf.pay.models.dto.PayHttpLogDTO;
import com.ztgf.pay.models.dto.PayUserDTO;
import com.ztgf.pay.models.service.IPayFundDetailsService;
import com.ztgf.pay.models.service.IPayHttpLogService;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.pay.models.vo.PayBalanceExpotPcVO;
import com.ztgf.pay.models.vo.PayBalanceExpotVO;
import com.ztgf.pay.models.vo.PayUserBalanceVO;
import com.ztgf.pay.models.vo.PayZjBalanceLogVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息表控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:13:56
 */
@DubboService
@Slf4j
public class PayUserServiceImpl extends ServiceImpl<PayUserMapper, PayUserDTO> implements IPayUserService{

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private IPayHttpLogService httpLogService;
    @Autowired
    private PayFundDetailsMapper payFundDetailsMapper;
    @Autowired
    private IPayFundDetailsService payFundDetailsService;

    @Value("${ztgf-income-user-id}")
    private Long ztgfIncomeUserId;//中铁工服收益账号

    @Override
    public Result<PayUserDTO> add(PayUserSaveBO saveBO) {
        if(saveBO.getUserId() == null || StrUtils.isEmpty(saveBO.getUserName())
                || StrUtils.isEmpty(saveBO.getLegalCredentialsType()) || StrUtils.isEmpty(saveBO.getUserMobile())
                || StrUtils.isEmpty(saveBO.getLegalCredentialsNo()) || StrUtils.isEmpty(saveBO.getBusinessLicenseNo())
        ){
            return new Result(CodeEnum.FAIL_100002);
        }

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("account_type", saveBO.getAccountType());
//        wrapper.and("user_id = {0} or user_name = {1} or legal_credentials_no = {2} or business_license_no = {3} or user_mobile = {4}",
//                saveBO.getUserId(), saveBO.getUserName(), saveBO.getLegalCredentialsNo(), saveBO.getBusinessLicenseNo(), saveBO.getUserMobile());
        wrapper.and("(user_id = {0} or user_name = {1} or business_license_no = {2})",
                saveBO.getUserId(), saveBO.getUserName(), saveBO.getBusinessLicenseNo());
        PayUserDTO dto = this.selectOne(wrapper);
        if(dto != null){
            return new Result(CodeEnum.FAIL_200001);
        }

        dto = new PayUserDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        // 2. 实例化交易对象
        TrdT1001Request trdRequest = new TrdT1001Request();
        trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));//交易日期：YYYYMMDD
        trdRequest.setSrl_ptnsrl(id.toString());//合作方交易流水号
        trdRequest.setCltacc_cltno(id.toString());//客户号（客户在合作方系统 中的唯一编号）
//        trdRequest.setCltacc_subno(cltacc_subno);//资金账号 FcFlg=2/3 时必填
        trdRequest.setCltacc_cltnm(saveBO.getUserName()); // 户名
        trdRequest.setClt_nm(saveBO.getLegalName());//姓名(当 Clt. Kd=0 时，该 节点的内容必须与节点 CltAcc. CltNm 的内容一 致) 公司：法定代表人 个人：自然人
        trdRequest.setClt_kd(saveBO.getType().toString());//客户性质(0 个人;1 公司)
        trdRequest.setClt_cdtp(saveBO.getLegalCredentialsType());//法定代表人/自然人 证件类型(详见数据字典)
        trdRequest.setClt_cdno(saveBO.getLegalCredentialsNo());//法定代表人/自然人 证件号码
        trdRequest.setClt_uscid(saveBO.getBusinessLicenseNo());//统一社会信用代码(公司必 填)

        trdRequest.setClt_mobno(saveBO.getUserMobile());//手机号码 BkCd=HXYH0001 且个人开户 时，手机号要填成个人银行 卡的预留手机号，否则无法 绑卡（T1004）
        trdRequest.setClt_email(saveBO.getUserEmail());//电子邮箱 BkCd=HXYH0001 时必填
        trdRequest.setClt_addr(saveBO.getUserAddress());//地址BkCd=HXYH0001 且 Kd=1 时 必填

        trdRequest.setFcflg("1");//业务功能标示(1:开户、2： 修改、3：销户)
        trdRequest.setAcctp(saveBO.getAccountType().toString());//账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)

        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        String msg = CodeEnum.FAIL_200002.getMsg();
        try{
            // 3. 报文处理
            trdRequest.process();
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setWaterId(id);
            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1001Response trdResponse = new TrdT1001Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());

            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("合作方流水号[srl_ptnsrl]=[" + trdResponse.getSrl_ptnsrl() + "]"); // 合作方流水号
                System.out.println("平台流水号[srl_platsrl]=[" + trdResponse.getSrl_platsrl() + "]"); // 平台流水号
                System.out.println("合作方客户号[cltacc_cltno]=[" + trdResponse.getCltacc_cltno() + "]"); // 合作方客户号
                System.out.println("平台客户号[cltacc_cltpid]=[" + trdResponse.getCltacc_cltpid() + "]"); // 平台客户号
                System.out.println("资金账号[cltacc_subno]=[" + trdResponse.getCltacc_subno() + "]"); // 资金账号
                System.out.println("账户名称[cltacc_cltnm]=[" + trdResponse.getCltacc_cltnm() + "]"); // 账户名称
                // System.out.println("[cltacc_bnkvid]=[" + trdResponse.getCltacc_bnkvid() +
                // "]"); // 银行虚拟账号
                System.out.println("银行电子账号（跨行收款账号）[cltacc_bnkeid]=[" + trdResponse.getCltacc_bnkeid() + "]"); // 银行电子账号（跨行收款账号）
                System.out.println("电子账户归属支行号[cltacc_openbkcd]=[" + trdResponse.getCltacc_openbkcd() + "]"); // 电子账户归属支行号
                System.out.println("电子账户归属支行名称[cltacc_openbknm]=[" + trdResponse.getCltacc_openbknm() + "]"); // 电子账户归属支行名称
                System.out.println("账户码[cltacc_acctcd]=[" + trdResponse.getCltacc_acctcd() + "]"); // 账户码

                dto.setZjAccountNo(trdResponse.getCltacc_acctcd());//中金账户码
                dto.setZjAccountName(trdResponse.getCltacc_cltnm());//中金户名
                dto.setZjMoneyNo(trdResponse.getCltacc_subno());//中金资金账号
                dto.setZjPlatNo(trdResponse.getCltacc_cltpid());//中金平台客户号
                dto.setZjClientNo(trdResponse.getCltacc_cltno());//中金客户号
                dto.setZjBankNo(trdResponse.getCltacc_openbkcd());//中金电子账户归属支行号
                dto.setZjBankName(trdResponse.getCltacc_openbknm());//中金电子账号归属支行名称
                dto.setZjBankElectronicNo(trdResponse.getCltacc_bnkeid());//中金银行电子账号（跨行收款账号）
                httpSuccess = true;
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

            // 显示返回报文
            logDTO.setStatus(0);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1001Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1001Request 开户 500");
        }
        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200002.getCode(), msg);
        } else {
            this.insert(dto);
        }
        return new Result(dto);
    }

    @Override
    public Result edit(PayUserSaveBO saveBO) {
        if(saveBO.getUserId() == null || StrUtils.isEmpty(saveBO.getUserName()) || StrUtils.isEmpty(saveBO.getLegalCredentialsNo()) || StrUtils.isEmpty(saveBO.getBusinessLicenseNo()) || StrUtils.isEmpty(saveBO.getUserMobile())){
            return new Result(CodeEnum.FAIL_100002);
        }

        PayUserDTO dto = getPayUserDTO(saveBO.getUserId(), saveBO.getAccountType());
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        BeanUtils.copyProperties(saveBO,dto);

        // 2. 实例化交易对象
        TrdT1001Request trdRequest = new TrdT1001Request();
        trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));//交易日期：YYYYMMDD
        trdRequest.setSrl_ptnsrl(ymlConfig.getPkId().toString());//合作方交易流水号
        trdRequest.setCltacc_cltno(dto.getZjClientNo());//客户号（客户在合作方系统 中的唯一编号）
        trdRequest.setCltacc_subno(dto.getZjMoneyNo());//资金账号 FcFlg=2/3 时必填
        trdRequest.setCltacc_cltnm(saveBO.getUserName()); // 户名
        trdRequest.setClt_nm(saveBO.getLegalName());//姓名(当 Clt. Kd=0 时，该 节点的内容必须与节点 CltAcc. CltNm 的内容一 致) 公司：法定代表人 个人：自然人
        trdRequest.setClt_kd(saveBO.getType().toString());//客户性质(0 个人;1 公司)
        trdRequest.setClt_cdtp(saveBO.getLegalCredentialsType());//法定代表人/自然人 证件类型(详见数据字典)
        trdRequest.setClt_cdno(saveBO.getLegalCredentialsNo());//法定代表人/自然人 证件号码
        trdRequest.setClt_uscid(saveBO.getBusinessLicenseNo());//统一社会信用代码(公司必 填)

        trdRequest.setClt_mobno(saveBO.getUserMobile());//手机号码 BkCd=HXYH0001 且个人开户 时，手机号要填成个人银行 卡的预留手机号，否则无法 绑卡（T1004）
        trdRequest.setClt_email(saveBO.getUserEmail());//电子邮箱 BkCd=HXYH0001 时必填
        trdRequest.setClt_addr(saveBO.getUserAddress());//地址BkCd=HXYH0001 且 Kd=1 时 必填

        trdRequest.setFcflg("2");//业务功能标示(1:开户、2： 修改、3：销户)
        trdRequest.setAcctp(saveBO.getAccountType().toString());//账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)

        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        String msg = CodeEnum.FAIL_200005.getMsg();
        try{
            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());

            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1001Response trdResponse = new TrdT1001Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setStatus(0);

            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("合作方流水号[srl_ptnsrl]=[" + trdResponse.getSrl_ptnsrl() + "]"); // 合作方流水号
                System.out.println("平台流水号[srl_platsrl]=[" + trdResponse.getSrl_platsrl() + "]"); // 平台流水号
                System.out.println("合作方客户号[cltacc_cltno]=[" + trdResponse.getCltacc_cltno() + "]"); // 合作方客户号
                System.out.println("平台客户号[cltacc_cltpid]=[" + trdResponse.getCltacc_cltpid() + "]"); // 平台客户号
                System.out.println("资金账号[cltacc_subno]=[" + trdResponse.getCltacc_subno() + "]"); // 资金账号
                System.out.println("账户名称[cltacc_cltnm]=[" + trdResponse.getCltacc_cltnm() + "]"); // 账户名称
                // System.out.println("[cltacc_bnkvid]=[" + trdResponse.getCltacc_bnkvid() +
                // "]"); // 银行虚拟账号
                System.out.println("银行电子账号（跨行收款账号）[cltacc_bnkeid]=[" + trdResponse.getCltacc_bnkeid() + "]"); // 银行电子账号（跨行收款账号）
                System.out.println("电子账户归属支行号[cltacc_openbkcd]=[" + trdResponse.getCltacc_openbkcd() + "]"); // 电子账户归属支行号
                System.out.println("电子账户归属支行名称[cltacc_openbknm]=[" + trdResponse.getCltacc_openbknm() + "]"); // 电子账户归属支行名称
                System.out.println("账户码[cltacc_acctcd]=[" + trdResponse.getCltacc_acctcd() + "]"); // 账户码

                dto.setZjAccountNo(trdResponse.getCltacc_acctcd());//中金账户码
                dto.setZjAccountName(trdResponse.getCltacc_cltnm());//中金户名
                dto.setZjMoneyNo(trdResponse.getCltacc_subno());//中金资金账号
                dto.setZjPlatNo(trdResponse.getCltacc_cltpid());//中金平台客户号
                dto.setZjClientNo(trdResponse.getCltacc_cltno());//中金客户号
                dto.setZjBankNo(trdResponse.getCltacc_openbkcd());//中金电子账户归属支行号
                dto.setZjBankName(trdResponse.getCltacc_openbknm());//中金电子账号归属支行名称
                dto.setZjBankElectronicNo(trdResponse.getCltacc_bnkeid());//中金银行电子账号（跨行收款账号）
                httpSuccess = true;
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1001Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1001Request 变更账户信息 500");
        }
        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200005.getCode(), msg);
        } else {
            this.updateById(dto);
        }
        return new Result();
    }

    /**
     *
     * @param userId
     * @param accountType 账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)
     * @return
     */
    @Override
    public Result del(Long userId, Integer accountType) {
        if(userId == null){
            return new Result(CodeEnum.FAIL_100002);
        }

        PayUserDTO dto = getPayUserDTO(userId, accountType);
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }


        // 2. 实例化交易对象
        TrdT1001Request trdRequest = new TrdT1001Request();
        trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));//交易日期：YYYYMMDD
        trdRequest.setSrl_ptnsrl(ymlConfig.getPkId().toString());//合作方交易流水号
        trdRequest.setCltacc_cltno(dto.getZjClientNo());//客户号（客户在合作方系统 中的唯一编号）
        trdRequest.setCltacc_subno(dto.getZjMoneyNo());//资金账号 FcFlg=2/3 时必填

        trdRequest.setFcflg("3");//业务功能标示(1:开户、2： 修改、3：销户)

        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        String msg = CodeEnum.FAIL_200004.getMsg();
        try{
            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1001Response trdResponse = new TrdT1001Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
            }

            // 显示返回报文

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1001Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1001Request 销户 500");
        }
        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200004.getCode(), msg);
        } else {
            this.updateById(dto);
        }
        return new Result();
    }

    @Override
    public Result<PayUserBalanceVO> getUserBalance(Long userId, Integer accountType) {
        if(userId == null || accountType == null){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(userId, accountType);
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        System.out.println("====getUserBalance========headBO:" + JSON.toJSONString(CommonParameter.getHeadParamsBO()));
        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        PayUserBalanceVO balanceVO = new PayUserBalanceVO();
        balanceVO.setUserId(userId);
        balanceVO.setUserName(dto.getUserName());
        balanceVO.setUserNo(dto.getZjMoneyNo());
        balanceVO.setBankNo(dto.getZjBankElectronicNo());
        balanceVO.setBankName(dto.getZjBankName());
        balanceVO.setNoWithdrawMoney(dto.getNoWithdrawMoney());
        String msg = CodeEnum.FAIL_200010.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT1005Request trdRequest = new TrdT1005Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
            trdRequest.setCltacc_subno(dto.getZjMoneyNo()); /** 资金账号 */
            trdRequest.setCltacc_cltnm(dto.getZjAccountName()); /** 户名 */

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            System.out.println(respMsg);
            TrdT1005Response trdResponse = new TrdT1005Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");
            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[amt_balamt]=[" + trdResponse.getAmt_balamt() + "]分"); // 资金余额
                System.out.println("[amt_useamt]=[" + trdResponse.getAmt_useamt() + "]分"); // 可用资金
                System.out.println("[amt_frzamt]=[" + trdResponse.getAmt_frzamt() + "]分"); // 冻结资金

                balanceVO.setBalanceMoney(BigDecimalUtils.divide(trdResponse.getAmt_balamt(), 100));
                balanceVO.setFreezeMoney(BigDecimalUtils.divide(trdResponse.getAmt_frzamt(), 100));
                balanceVO.setUseMoney(BigDecimalUtils.divide(trdResponse.getAmt_useamt(), 100));

                httpSuccess = true;

            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

            // 显示返回报文
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1005Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1005Request 获取用户余额 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
//        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200010.getCode(), msg);
        }
        return new Result(balanceVO);
    }


    /**
     * 查询往来账明细(当前日期)[T9007]
     * @param bo
     * @return
     */
    @Override
    public Result<Page<PayZjBalanceLogVO>> queryPageZjBalanceLog(PayZjBalanceLogBO bo) {

        PayUserDTO dto = new PayUserDTO();
        if(bo.getUserId() != null){
            dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
            if(dto == null){
                return new Result(CodeEnum.FAIL_200003);
            }
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        Page<PayZjBalanceLogVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());

        String msg = CodeEnum.FAIL_200011.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT9007Request trdRequest = new TrdT9007Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
//            trdRequest.setQuystdt(DateUtils.dateToStr(bo.getStartDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));     /** 起始日期YYYYMMDD */
//            trdRequest.setQuyenddt(DateUtils.dateToStr(bo.getStartDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));  /** 结束日期YYYYMMDD */
            trdRequest.setSubno(dto.getZjMoneyNo());       /** 资金账号 */
            trdRequest.setTye(bo.getType());           /** 类型(C:增加、T:减少) */
            trdRequest.setBsitye(bo.getBusinessType());/** 业务类型: INMNY入金; OTMNY出金; FRZE冻结; UNFRZE解冻; PAYMNY付款; RCVMNY收款 支持同时传入多个值，使用半角英文逗号隔开*/
            trdRequest.setDtrdtype(bo.getOriginalBusinessType());/*原交易-类型: TNSTRD出入金业务 PAYTRD支付业务 FRETRD冻结解冻业务 SHFKTRD收款业务,支持同时传入多个值，使用半角英文逗号隔开 */
            trdRequest.setPagsiz(bo.getPageSize()); /** 分页查询：每页N条数据(10<=N<=200) */
            trdRequest.setCurpag(bo.getPageNo()); /** 分页查询：当前页数 */
            trdRequest.setSortr(bo.getSort().toString());/** 排序规则:10明细编号-正序（默认）;11明细编号-倒序 */

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            log.error("-----请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT9007Response trdResponse = new TrdT9007Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");
            log.error("-----响应报文[" + trdResponse.getResponsePlainText() + "]");
            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            List<PayZjBalanceLogVO> list = new ArrayList<>();
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[talpag]=[" + trdResponse.getTalpag() + "]"); // 分页查询：总页数
                System.out.println("[curpag]=[" + trdResponse.getCurpag() + "]"); // 分页查询：当前页数
                System.out.println("[talrcd]=[" + trdResponse.getTalrcd() + "]"); // 分页查询：总条数

                page.setTotal(Integer.parseInt(trdResponse.getTalrcd() + ""));
                page.setCurrent(Integer.parseInt(trdResponse.getCurpag() + ""));
                page.setSize(bo.getPageSize());

                List<QuyDaT9007> quyDatList = trdResponse.getQuyDatList();
                if (null != quyDatList && !quyDatList.isEmpty()) {
                    for (QuyDaT9007 quyDat : quyDatList) {
                        System.out.println("==============================================");
                        System.out.println("[dte]=[" + quyDat.getDte() + "]"); // 日期YYYYMMDD
                        System.out.println("[tme]=[" + quyDat.getTme() + "]"); // 时间HH24MISS
                        System.out.println("[mnychgno]=[" + quyDat.getMnychgno() + "]"); // 明细编号:默认按照此字段正序排序
                        System.out.println("[subno]=[" + quyDat.getSubno() + "]"); // 资金账号
                        System.out.println("[tye]=[" + quyDat.getTye() + "]"); // 类型(C:增加、T:减少)
                        System.out.println("[rsubno]=[" + quyDat.getRsubno() + "]"); // 对方-账号
                        System.out.println("[rcltnm]=[" + quyDat.getRcltnm() + "]"); // 对方-户名
                        System.out.println("[aclamt]=[" + quyDat.getAclamt() + "]分"); // 发生额
                        System.out.println("[feeamt]=[" + quyDat.getFeeamt() + "]分"); // 手续费
                        System.out.println("[balamt]=[" + quyDat.getBalamt() + "]分"); // 资金变动后的可用资金余额
                        // 业务类型
                        // INMNY入金;
                        // OTMNY出金;
                        // FRZE冻结;
                        // UNFRZE解冻;
                        // PAYMNY付款;
                        // RCVMNY收款
                        System.out.println("[bsitye]=[" + quyDat.getBsitye() + "]");
                        System.out.println("[bsidsc]=[" + quyDat.getBsidsc() + "]"); // 业务描述

                        // 原交易-类型
                        // TNSTRD出入金业务
                        // PAYTRD支付业务
                        // FRETRD冻结解冻业务
                        // SHFKTRD收款业务
                        System.out.println("[dtrdtype]=[" + quyDat.getDtrdtype() + "]");
                        System.out.println("[dptnsrl]=[" + quyDat.getDptnsrl() + "]"); // 原交易-合作方交易流水号
                        System.out.println("[dplatsrl]=[" + quyDat.getDplatsrl() + "]"); // 原交易-平台交易流水号
                        System.out.println("[usage]=[" + quyDat.getUsage() + "]"); // 资金用途(附言)
                        System.out.println("[ccycd]=[" + quyDat.getCcycd() + "]"); // 币种，默认“CNY”
                        System.out.println("==============================================");

                        PayZjBalanceLogVO logVO = new PayZjBalanceLogVO();
                        if(bo.getUserId() == null){
                            dto = getPayUserDTO(quyDat.getSubno());
                        }
                        logVO.setUserId(dto.getUserId());//
                        logVO.setUserName(dto.getUserName());//
                        logVO.setUserNo(dto.getZjMoneyNo());//

                        logVO.setAccountingDate(quyDat.getDte());//记账日期 YYYYMMDD
                        logVO.setAccountingTime(quyDat.getTme());//记账时间 HH24MISS
                        logVO.setDataNo(quyDat.getMnychgno());//明细编号 默认按照此字段正序排序
                        logVO.setType(quyDat.getTye());//类型(C:增加、T:减少)
                        logVO.setOtherUserNo(quyDat.getRsubno());//对方-账号
                        logVO.setOtherUserName(quyDat.getRcltnm());//对方-户名
                        logVO.setMoeny(BigDecimalUtils.divide(quyDat.getAclamt(), 100));//发生额
                        logVO.setFeeMoney(BigDecimalUtils.divide(quyDat.getFeeamt(), 100));//手续费
                        logVO.setUseMoney(BigDecimalUtils.divide(quyDat.getBalamt(), 100));//资金变动后的可用资金余额
                        logVO.setBusinessType(quyDat.getBsitye());//业务类型 INMNY 入金; OTMNY 出金; FRZE 冻结; UNFRZE 解冻; PAYMNY 付款; RCVMNY 收款
                        logVO.setBusinessNotes(quyDat.getBsidsc());//业务描述
                        logVO.setOriginalBusinessType(quyDat.getDtrdtype());//原交易-类型 TNSTRD 出入金业务 PAYTRD 支付业务 FRETRD 冻结解冻业务 SHFKTRD 收款业务
                        logVO.setOriginalBusinessId(quyDat.getDptnsrl());//原交易-合作方交易流水号
                        logVO.setOriginalZjBusinessId(quyDat.getDplatsrl());//原交易-平台交易流水号
                        logVO.setMoneyUsage(quyDat.getUsage());//资金用途(附言)
                        list.add(logVO);
                    }
                }

                page.setRecords(list);
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT9008Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT9008Request 获取交易流水 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
//        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200011.getCode(), msg);
        }
        return new Result(page);
    }

    @Override
    public List<PayZjBalanceLogVO> queryPageZjBalance(PayZjBalanceLogBO bo) {

        if(bo.getUserId() == null){
            return null;
        }

        PayUserDTO dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
        if(dto == null){
            return null;
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;

        String msg = CodeEnum.FAIL_200011.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT9007Request trdRequest = new TrdT9007Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
            trdRequest.setSubno(dto.getZjMoneyNo());       /** 资金账号 */
            trdRequest.setTye(bo.getType());           /** 类型(C:增加、T:减少) */
            trdRequest.setBsitye(bo.getBusinessType());/** 业务类型: INMNY入金; OTMNY出金; FRZE冻结; UNFRZE解冻; PAYMNY付款; RCVMNY收款 支持同时传入多个值，使用半角英文逗号隔开*/
            trdRequest.setDtrdtype(bo.getOriginalBusinessType());/*原交易-类型: TNSTRD出入金业务 PAYTRD支付业务 FRETRD冻结解冻业务 SHFKTRD收款业务,支持同时传入多个值，使用半角英文逗号隔开 */
            trdRequest.setSortr(bo.getSort().toString());/** 排序规则:10明细编号-正序（默认）;11明细编号-倒序 */

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT9007Response trdResponse = new TrdT9007Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            List<PayZjBalanceLogVO> list = new ArrayList<>();
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                List<QuyDaT9007> quyDatList = trdResponse.getQuyDatList();
                if (null != quyDatList && !quyDatList.isEmpty()) {
                    for (QuyDaT9007 quyDat : quyDatList) {
                        PayZjBalanceLogVO logVO = new PayZjBalanceLogVO();
                        logVO.setUserId(dto.getUserId());//
                        logVO.setUserName(dto.getUserName());//
                        logVO.setUserNo(dto.getZjMoneyNo());//

                        logVO.setAccountingDate(quyDat.getDte());//记账日期 YYYYMMDD
                        logVO.setAccountingTime(quyDat.getTme());//记账时间 HH24MISS
                        logVO.setDataNo(quyDat.getMnychgno());//明细编号 默认按照此字段正序排序
                        logVO.setType(quyDat.getTye());//类型(C:增加、T:减少)
                        logVO.setOtherUserNo(quyDat.getRsubno());//对方-账号
                        logVO.setOtherUserName(quyDat.getRcltnm());//对方-户名
                        logVO.setMoeny(BigDecimalUtils.divide(quyDat.getAclamt(), 100));//发生额
                        logVO.setFeeMoney(BigDecimalUtils.divide(quyDat.getFeeamt(), 100));//手续费
                        logVO.setUseMoney(BigDecimalUtils.divide(quyDat.getBalamt(), 100));//资金变动后的可用资金余额
                        logVO.setBusinessType(quyDat.getBsitye());//业务类型 INMNY 入金; OTMNY 出金; FRZE 冻结; UNFRZE 解冻; PAYMNY 付款; RCVMNY 收款
                        logVO.setBusinessNotes(quyDat.getBsidsc());//业务描述
                        logVO.setOriginalBusinessType(quyDat.getDtrdtype());//原交易-类型 TNSTRD 出入金业务 PAYTRD 支付业务 FRETRD 冻结解冻业务 SHFKTRD 收款业务
                        logVO.setOriginalBusinessId(quyDat.getDptnsrl());//原交易-合作方交易流水号
                        logVO.setOriginalZjBusinessId(quyDat.getDplatsrl());//原交易-平台交易流水号
                        logVO.setMoneyUsage(quyDat.getUsage());//资金用途(附言)
                        list.add(logVO);
                    }
                }
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT9008Request is error", e);
        }
        return null;
    }

    /**
     * 查询往来账明细(历史日期)[T9008]
     * @param bo
     * @return
     */
   @Override
    public Result<Page<PayZjBalanceLogVO>> queryPageZjHistoryBalanceLog(PayZjBalanceLogBO bo) {
//        if(bo.getUserId() == null){
//            return new Result(CodeEnum.FAIL_100002);
//        }
//        PayUserDTO dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
//        if(dto == null){
//            return new Result(CodeEnum.FAIL_200003);
//        }

        PayUserDTO dto = new PayUserDTO();
        if(bo.getUserId() != null){
            dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
            if(dto == null){
                return new Result(CodeEnum.FAIL_200003);
            }
        }


        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        Page<PayZjBalanceLogVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());

        String msg = CodeEnum.FAIL_200011.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT9008Request trdRequest = new TrdT9008Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
            trdRequest.setQuystdt(DateUtils.dateToStr(bo.getStartDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));     //** 起始日期YYYYMMDD *//*
            trdRequest.setQuyenddt(DateUtils.dateToStr(bo.getEndDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));  //** 结束日期YYYYMMDD *//*
            if(StrUtils.isNotEmpty(dto.getZjMoneyNo())){
                trdRequest.setSubno(dto.getZjMoneyNo());       /** 资金账号 */
            }
            trdRequest.setTye(bo.getType());           //** 类型(C:增加、T:减少) *//*
            trdRequest.setBsitye(bo.getBusinessType());//** 业务类型: INMNY入金; OTMNY出金; FRZE冻结; UNFRZE解冻; PAYMNY付款; RCVMNY收款 支持同时传入多个值，使用半角英文逗号隔开*//*
            trdRequest.setDtrdtype(bo.getOriginalBusinessType());//*原交易-类型: TNSTRD出入金业务 PAYTRD支付业务 FRETRD冻结解冻业务 SHFKTRD收款业务,支持同时传入多个值，使用半角英文逗号隔开 *//*
            trdRequest.setPagsiz(bo.getPageSize()); //** 分页查询：每页N条数据(10<=N<=200) *//*
            trdRequest.setCurpag(bo.getPageNo()); //** 分页查询：当前页数 *//*
            trdRequest.setSortr(bo.getSort().toString());//** 排序规则:10明细编号-正序（默认）;11明细编号-倒序 *//*

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT9008Response trdResponse = new TrdT9008Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            List<PayZjBalanceLogVO> list = new ArrayList<>();
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[talpag]=[" + trdResponse.getTalpag() + "]"); // 分页查询：总页数
                System.out.println("[curpag]=[" + trdResponse.getCurpag() + "]"); // 分页查询：当前页数
                System.out.println("[talrcd]=[" + trdResponse.getTalrcd() + "]"); // 分页查询：总条数

                page.setTotal(Integer.parseInt(trdResponse.getTalrcd() + ""));
                page.setCurrent(Integer.parseInt(trdResponse.getCurpag() + ""));
                page.setSize(bo.getPageSize());

                List<QuyDaT9008> quyDatList = trdResponse.getQuyDatList();
                if (null != quyDatList && !quyDatList.isEmpty()) {
                    for (QuyDaT9008 quyDat : quyDatList) {
                        System.out.println("==============================================");
                        System.out.println("[dte]=[" + quyDat.getDte() + "]"); // 日期YYYYMMDD
                        System.out.println("[tme]=[" + quyDat.getTme() + "]"); // 时间HH24MISS
                        System.out.println("[mnychgno]=[" + quyDat.getMnychgno() + "]"); // 明细编号:默认按照此字段正序排序
                        System.out.println("[subno]=[" + quyDat.getSubno() + "]"); // 资金账号
                        System.out.println("[tye]=[" + quyDat.getTye() + "]"); // 类型(C:增加、T:减少)
                        System.out.println("[rsubno]=[" + quyDat.getRsubno() + "]"); // 对方-账号
                        System.out.println("[rcltnm]=[" + quyDat.getRcltnm() + "]"); // 对方-户名
                        System.out.println("[aclamt]=[" + quyDat.getAclamt() + "]分"); // 发生额
                        System.out.println("[feeamt]=[" + quyDat.getFeeamt() + "]分"); // 手续费
                        System.out.println("[balamt]=[" + quyDat.getBalamt() + "]分"); // 资金变动后的可用资金余额
                        // 业务类型
                        // INMNY入金;
                        // OTMNY出金;
                        // FRZE冻结;
                        // UNFRZE解冻;
                        // PAYMNY付款;
                        // RCVMNY收款
                        System.out.println("[bsitye]=[" + quyDat.getBsitye() + "]");
                        System.out.println("[bsidsc]=[" + quyDat.getBsidsc() + "]"); // 业务描述

                        // 原交易-类型
                        // TNSTRD出入金业务
                        // PAYTRD支付业务
                        // FRETRD冻结解冻业务
                        // SHFKTRD收款业务
                        System.out.println("[dtrdtype]=[" + quyDat.getDtrdtype() + "]");
                        System.out.println("[dptnsrl]=[" + quyDat.getDptnsrl() + "]"); // 原交易-合作方交易流水号
                        System.out.println("[dplatsrl]=[" + quyDat.getDplatsrl() + "]"); // 原交易-平台交易流水号
                        System.out.println("[usage]=[" + quyDat.getUsage() + "]"); // 资金用途(附言)
                        System.out.println("[ccycd]=[" + quyDat.getCcycd() + "]"); // 币种，默认“CNY”
                        System.out.println("==============================================");

                        PayZjBalanceLogVO logVO = new PayZjBalanceLogVO();
                        if(bo.getUserId() == null){
                            dto = getPayUserDTO(quyDat.getSubno());
                        }
                        logVO.setUserId(dto.getUserId());//
                        logVO.setUserName(dto.getUserName());//
                        logVO.setUserNo(dto.getZjMoneyNo());//
                        logVO.setAccountingDate(quyDat.getDte());//记账日期 YYYYMMDD
                        logVO.setAccountingTime(quyDat.getTme());//记账时间 HH24MISS
                        logVO.setDataNo(quyDat.getMnychgno());//明细编号 默认按照此字段正序排序
                        logVO.setType(quyDat.getTye());//类型(C:增加、T:减少)
                        logVO.setOtherUserNo(quyDat.getRsubno());//对方-账号
                        logVO.setOtherUserName(quyDat.getRcltnm());//对方-户名
                        logVO.setMoeny(BigDecimalUtils.divide(quyDat.getAclamt(), 100));//发生额
                        logVO.setFeeMoney(BigDecimalUtils.divide(quyDat.getFeeamt(), 100));//手续费
                        logVO.setUseMoney(BigDecimalUtils.divide(quyDat.getBalamt(), 100));//资金变动后的可用资金余额
                        logVO.setBusinessType(quyDat.getBsitye());//业务类型 INMNY 入金; OTMNY 出金; FRZE 冻结; UNFRZE 解冻; PAYMNY 付款; RCVMNY 收款
                        logVO.setBusinessNotes(quyDat.getBsidsc());//业务描述
                        logVO.setOriginalBusinessType(quyDat.getDtrdtype());//原交易-类型 TNSTRD 出入金业务 PAYTRD 支付业务 FRETRD 冻结解冻业务 SHFKTRD 收款业务
                        logVO.setOriginalBusinessId(quyDat.getDptnsrl());//原交易-合作方交易流水号
                        logVO.setOriginalZjBusinessId(quyDat.getDplatsrl());//原交易-平台交易流水号
                        logVO.setMoneyUsage(quyDat.getUsage());//资金用途(附言)
                        list.add(logVO);
                    }
                }

                page.setRecords(list);
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT9008Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT9008Request 获取交易流水 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
//        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200011.getCode(), msg);
        }
        return new Result(page);
    }
    @Override
    public Result<Page<PayZjBalanceLogVO>> queryPageZjHistoryBalanceLogAdmin(PayZjBalanceLogBO bo) {
        if(bo.getUserId() == null){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(bo.getUserId(), 3);
        if(dto == null){
                return new Result(CodeEnum.FAIL_200003);
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        Page<PayZjBalanceLogVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());

        String msg = CodeEnum.FAIL_200011.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT9008Request trdRequest = new TrdT9008Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
            trdRequest.setQuystdt(DateUtils.dateToStr(bo.getStartDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));     //** 起始日期YYYYMMDD *//*
            trdRequest.setQuyenddt(DateUtils.dateToStr(bo.getEndDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));  //** 结束日期YYYYMMDD *//*
            trdRequest.setSubno(dto.getZjMoneyNo());       /** 资金账号 */
            trdRequest.setTye(bo.getType());           //** 类型(C:增加、T:减少) *//*
            trdRequest.setBsitye(bo.getBusinessType());//** 业务类型: INMNY入金; OTMNY出金; FRZE冻结; UNFRZE解冻; PAYMNY付款; RCVMNY收款 支持同时传入多个值，使用半角英文逗号隔开*//*
            trdRequest.setDtrdtype(bo.getOriginalBusinessType());//*原交易-类型: TNSTRD出入金业务 PAYTRD支付业务 FRETRD冻结解冻业务 SHFKTRD收款业务,支持同时传入多个值，使用半角英文逗号隔开 *//*
            trdRequest.setPagsiz(bo.getPageSize()); //** 分页查询：每页N条数据(10<=N<=200) *//*
            trdRequest.setCurpag(bo.getPageNo()); //** 分页查询：当前页数 *//*
            trdRequest.setSortr(bo.getSort().toString());//** 排序规则:10明细编号-正序（默认）;11明细编号-倒序 *//*

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT9008Response trdResponse = new TrdT9008Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            List<PayZjBalanceLogVO> list = new ArrayList<>();
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[talpag]=[" + trdResponse.getTalpag() + "]"); // 分页查询：总页数
                System.out.println("[curpag]=[" + trdResponse.getCurpag() + "]"); // 分页查询：当前页数
                System.out.println("[talrcd]=[" + trdResponse.getTalrcd() + "]"); // 分页查询：总条数

                page.setTotal(Integer.parseInt(trdResponse.getTalrcd() + ""));
                page.setCurrent(Integer.parseInt(trdResponse.getCurpag() + ""));
                page.setSize(bo.getPageSize());

                List<QuyDaT9008> quyDatList = trdResponse.getQuyDatList();
                if (null != quyDatList && !quyDatList.isEmpty()) {
                    for (QuyDaT9008 quyDat : quyDatList) {
                        System.out.println("==============================================");
                        System.out.println("[dte]=[" + quyDat.getDte() + "]"); // 日期YYYYMMDD
                        System.out.println("[tme]=[" + quyDat.getTme() + "]"); // 时间HH24MISS
                        System.out.println("[mnychgno]=[" + quyDat.getMnychgno() + "]"); // 明细编号:默认按照此字段正序排序
                        System.out.println("[subno]=[" + quyDat.getSubno() + "]"); // 资金账号
                        System.out.println("[tye]=[" + quyDat.getTye() + "]"); // 类型(C:增加、T:减少)
                        System.out.println("[rsubno]=[" + quyDat.getRsubno() + "]"); // 对方-账号
                        System.out.println("[rcltnm]=[" + quyDat.getRcltnm() + "]"); // 对方-户名
                        System.out.println("[aclamt]=[" + quyDat.getAclamt() + "]分"); // 发生额
                        System.out.println("[feeamt]=[" + quyDat.getFeeamt() + "]分"); // 手续费
                        System.out.println("[balamt]=[" + quyDat.getBalamt() + "]分"); // 资金变动后的可用资金余额
                        // 业务类型
                        // INMNY入金;
                        // OTMNY出金;
                        // FRZE冻结;
                        // UNFRZE解冻;
                        // PAYMNY付款;
                        // RCVMNY收款
                        System.out.println("[bsitye]=[" + quyDat.getBsitye() + "]");
                        System.out.println("[bsidsc]=[" + quyDat.getBsidsc() + "]"); // 业务描述

                        // 原交易-类型
                        // TNSTRD出入金业务
                        // PAYTRD支付业务
                        // FRETRD冻结解冻业务
                        // SHFKTRD收款业务
                        System.out.println("[dtrdtype]=[" + quyDat.getDtrdtype() + "]");
                        System.out.println("[dptnsrl]=[" + quyDat.getDptnsrl() + "]"); // 原交易-合作方交易流水号
                        System.out.println("[dplatsrl]=[" + quyDat.getDplatsrl() + "]"); // 原交易-平台交易流水号
                        System.out.println("[usage]=[" + quyDat.getUsage() + "]"); // 资金用途(附言)
                        System.out.println("[ccycd]=[" + quyDat.getCcycd() + "]"); // 币种，默认“CNY”
                        System.out.println("==============================================");

                        PayZjBalanceLogVO logVO = new PayZjBalanceLogVO();
                        logVO.setUserId(dto.getUserId());//
                        logVO.setUserName(dto.getUserName());//
                        logVO.setUserNo(dto.getZjMoneyNo());//

                        logVO.setAccountingDate(quyDat.getDte());//记账日期 YYYYMMDD
                        logVO.setAccountingTime(quyDat.getTme());//记账时间 HH24MISS
                        logVO.setDataNo(quyDat.getMnychgno());//明细编号 默认按照此字段正序排序
                        logVO.setType(quyDat.getTye());//类型(C:增加、T:减少)
                        logVO.setOtherUserNo(quyDat.getRsubno());//对方-账号
                        logVO.setOtherUserName(quyDat.getRcltnm());//对方-户名
                        logVO.setMoeny(BigDecimalUtils.divide(quyDat.getAclamt(), 100));//发生额
                        logVO.setFeeMoney(BigDecimalUtils.divide(quyDat.getFeeamt(), 100));//手续费
                        logVO.setUseMoney(BigDecimalUtils.divide(quyDat.getBalamt(), 100));//资金变动后的可用资金余额
                        logVO.setBusinessType(quyDat.getBsitye());//业务类型 INMNY 入金; OTMNY 出金; FRZE 冻结; UNFRZE 解冻; PAYMNY 付款; RCVMNY 收款
                        logVO.setBusinessNotes(quyDat.getBsidsc());//业务描述
                        logVO.setOriginalBusinessType(quyDat.getDtrdtype());//原交易-类型 TNSTRD 出入金业务 PAYTRD 支付业务 FRETRD 冻结解冻业务 SHFKTRD 收款业务
                        logVO.setOriginalBusinessId(quyDat.getDptnsrl());//原交易-合作方交易流水号
                        logVO.setOriginalZjBusinessId(quyDat.getDplatsrl());//原交易-平台交易流水号
                        logVO.setMoneyUsage(quyDat.getUsage());//资金用途(附言)
                        list.add(logVO);
                    }
                }

                page.setRecords(list);
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT9008Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT9008Request 获取交易流水 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200011.getCode(), msg);
        }
        return new Result(page);
    }
    @Override
    public Result<Page<PayFundDetailsDTO>> queryPageZjHistoryBalanceLog1(PayZjBalanceLogBO bo) {
        Page<PayFundDetailsDTO> page = new Page<>();
        page.setSize(bo.getPageSize());
        page.setCurrent(bo.getPageNo());
        List<PayFundDetailsDTO> list=payFundDetailsMapper.selectByPage(page,bo);
        page.setRecords(list);
        return  new Result<>(page);
    }



    @Override
    public Result addBankCard(PayUserBankcardSaveBO bo) {
        if(bo.getUserId() == null || StrUtils.isEmpty(bo.getBankId()) || StrUtils.isEmpty(bo.getBankCard()) || StrUtils.isEmpty(bo.getAccountBankNo())){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        String msg = CodeEnum.FAIL_200006.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT1004Request trdRequest = new TrdT1004Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));/** 交易日期 */
            trdRequest.setSrl_ptnsrl(bo.getId().toString());/** 合作方交易流水号 */
            trdRequest.setCltacc_subno(dto.getZjMoneyNo());/** 资金账号 */
            trdRequest.setCltacc_cltnm(dto.getZjAccountName()); /** 户名 */

//            trdRequest.setBkacc_souraccno(bkacc_souraccno);//原银行账号(卡号), FcFlg=2/3时填写原绑定银行卡号，如果只绑定一张结算银行卡 ，可以不填写
            trdRequest.setBkacc_bkid(bo.getBankId());/** 银行编号 */
            trdRequest.setBkacc_accno(bo.getBankCard());/** 银行账号(卡号) */
            trdRequest.setBkacc_accnm(dto.getZjAccountName()); /** 开户名称 */
            trdRequest.setBkacc_acctp("1");/** 账户类别(1公司;2个人) */
            /**
             * 银行账户(卡)类型 <br>
             * 1:个人借记卡(储蓄卡)，默认 <br>
             * 2:个人贷记卡(信用卡) <br>
             * 3:个人电子账户(银行二类户) <br>
             * A:企业一般结算账户，默认 <br>
             * B:企业电子账户
             */
            trdRequest.setBkacc_crdtp("A");
            trdRequest.setBkacc_cdtp("G");/** 开户证件类型 A身份证、B户口簿、C军官证、D警官证、E护照、F港澳通行证、G社会统一信用代码、H营业执照、I文职干部证、J士兵证、K台湾通行证、L其他*/
            trdRequest.setBkacc_cdno(dto.getBusinessLicenseNo());/** 证件号码 */
            trdRequest.setBkacc_phone(bo.getMobile());/** 银行预留手机号码 */
            if("302".equals(bo.getBankId())){
                trdRequest.setBkacc_crsmk("1");/** 跨行标示(1：本行 2:跨行) */
            } else {
                trdRequest.setBkacc_crsmk("2");/** 跨行标示(1：本行 2:跨行) */
            }

            trdRequest.setBkacc_openbkcd(bo.getAccountBankNo());/** 开户网点编号 (人行分配的12位编号) */
            trdRequest.setActiflag("4");//激活方式 1 企业主动打款激活; 3 短信验证激活; 4 企业被动打款激活
            trdRequest.setFcflg("1");//业务功能标示(1:绑定、2： 变更、3：删除)


            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1004Response trdResponse = new TrdT1004Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[srl_ptnsrl]=[" + trdResponse.getSrl_ptnsrl() + "]"); // 合作方流水号
                System.out.println("[srl_platsrl]=[" + trdResponse.getSrl_platsrl() + "]"); // 平台流水号
                // 绑定/变更/删除结果（RspCode=000000时返回）：
                // 1绑定/变更/删除成功（此模式下无需验证）
                // 2申请绑定/变更成功，需人工审核激活
                // 3申请绑定/变更成功，需要短信验证
                // 4申请绑定/变更成功，需要打款验证（被动打款）
                // 5申请绑定/变更成功，需要异步网关确认 <br>
                // 9绑定/变更/删除失败
                //添加日志
                logDTO.setId(ymlConfig.getPkId());
                httpLogService.insert(logDTO);
                if("1".equals(trdResponse.getState())){
                    return new Result();
                } else if("2".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("3".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200013);
                } else if("4".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200014);
                } else if("5".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200015);
                } else if("9".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200006);
                } else {
                    return new Result(CodeEnum.FAIL_200016);
                }
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1004Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1004Request 绑卡 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200006.getCode(), msg);
        }
        return new Result();
    }

    @Override
    public Result editBankCard(PayUserBankcardSaveBO bo) {
        if(bo.getUserId() == null || StrUtils.isEmpty(bo.getBankId()) || StrUtils.isEmpty(bo.getBankCard()) || StrUtils.isEmpty(bo.getAccountBankNo())){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        String msg = CodeEnum.FAIL_200006.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT1004Request trdRequest = new TrdT1004Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));/** 交易日期 */
            trdRequest.setSrl_ptnsrl(ymlConfig.getPkId().toString());/** 合作方交易流水号 */
            trdRequest.setCltacc_subno(dto.getZjMoneyNo());/** 资金账号 */
            trdRequest.setCltacc_cltnm(dto.getZjAccountName()); /** 户名 */

//            trdRequest.setBkacc_souraccno(bkacc_souraccno);//原银行账号(卡号), FcFlg=2/3时填写原绑定银行卡号，如果只绑定一张结算银行卡 ，可以不填写
            trdRequest.setBkacc_bkid(bo.getBankId());/** 银行编号 */
            trdRequest.setBkacc_accno(bo.getBankCard());/** 银行账号(卡号) */
            trdRequest.setBkacc_accnm(dto.getZjAccountName()); /** 开户名称 */
            trdRequest.setBkacc_acctp("1");/** 账户类别(1公司;2个人) */
            /**
             * 银行账户(卡)类型 <br>
             * 1:个人借记卡(储蓄卡)，默认 <br>
             * 2:个人贷记卡(信用卡) <br>
             * 3:个人电子账户(银行二类户) <br>
             * A:企业一般结算账户，默认 <br>
             * B:企业电子账户
             */
            trdRequest.setBkacc_crdtp("A");
            trdRequest.setBkacc_cdtp("G");/** 开户证件类型 A身份证、B户口簿、C军官证、D警官证、E护照、F港澳通行证、G社会统一信用代码、H营业执照、I文职干部证、J士兵证、K台湾通行证、L其他*/
            trdRequest.setBkacc_cdno(dto.getBusinessLicenseNo());/** 证件号码 */
            trdRequest.setBkacc_phone(bo.getMobile());/** 银行预留手机号码 */
            trdRequest.setBkacc_crsmk("2");/** 跨行标示(1：本行 2:跨行) */
            trdRequest.setBkacc_openbkcd(bo.getAccountBankNo());/** 开户网点编号 (人行分配的12位编号) */
            trdRequest.setActiflag("4");//激活方式 1 企业主动打款激活; 3 短信验证激活; 4 企业被动打款激活
            trdRequest.setFcflg("2");//业务功能标示(1:绑定、2： 变更、3：删除)


            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1004Response trdResponse = new TrdT1004Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[srl_ptnsrl]=[" + trdResponse.getSrl_ptnsrl() + "]"); // 合作方流水号
                System.out.println("[srl_platsrl]=[" + trdResponse.getSrl_platsrl() + "]"); // 平台流水号
                // 绑定/变更/删除结果（RspCode=000000时返回）：
                // 1绑定/变更/删除成功（此模式下无需验证）
                // 2申请绑定/变更成功，需人工审核激活
                // 3申请绑定/变更成功，需要短信验证
                // 4申请绑定/变更成功，需要打款验证（被动打款）
                // 5申请绑定/变更成功，需要异步网关确认 <br>
                // 9绑定/变更/删除失败
                System.out.println("[state]=[" + trdResponse.getState() + "]");
                System.out.println("[actiinfo]=[" + trdResponse.getActiinfo() + "]");
                //添加日志
                logDTO.setId(ymlConfig.getPkId());
                httpLogService.insert(logDTO);
                if("1".equals(trdResponse.getState())){
                    return new Result();
                } else if("2".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("3".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("4".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("5".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("9".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200006);
                } else {
                    return new Result(CodeEnum.FAIL_200016);
                }
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1004Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1004Request 换绑 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200006.getCode(), msg);
        }
        return new Result();
    }

    @Override
    public Result delBankCard(Long userId, Integer accountType,  String bankCard) {
        if(accountType == null || userId == null || StrUtils.isEmpty(bankCard)){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(userId, accountType);
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        String msg = CodeEnum.FAIL_200006.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT1004Request trdRequest = new TrdT1004Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));/** 交易日期 */
            trdRequest.setSrl_ptnsrl(ymlConfig.getPkId().toString());/** 合作方交易流水号 */
            trdRequest.setCltacc_subno(dto.getZjMoneyNo());/** 资金账号 */
            trdRequest.setCltacc_cltnm(dto.getZjAccountName()); /** 户名 */

            trdRequest.setBkacc_souraccno(bankCard);//原银行账号(卡号), FcFlg=2/3时填写原绑定银行卡号，如果只绑定一张结算银行卡 ，可以不填写

            trdRequest.setFcflg("3");//业务功能标示(1:绑定、2： 变更、3：删除)


            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1004Response trdResponse = new TrdT1004Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[srl_ptnsrl]=[" + trdResponse.getSrl_ptnsrl() + "]"); // 合作方流水号
                System.out.println("[srl_platsrl]=[" + trdResponse.getSrl_platsrl() + "]"); // 平台流水号
                // 绑定/变更/删除结果（RspCode=000000时返回）：
                // 1绑定/变更/删除成功（此模式下无需验证）
                // 2申请绑定/变更成功，需人工审核激活
                // 3申请绑定/变更成功，需要短信验证
                // 4申请绑定/变更成功，需要打款验证（被动打款）
                // 5申请绑定/变更成功，需要异步网关确认 <br>
                // 9绑定/变更/删除失败
                System.out.println("[state]=[" + trdResponse.getState() + "]");
                System.out.println("[actiinfo]=[" + trdResponse.getActiinfo() + "]");
                //添加日志
                logDTO.setId(ymlConfig.getPkId());
                httpLogService.insert(logDTO);
                if("1".equals(trdResponse.getState())){
                    return new Result();
                } else if("2".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("3".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("4".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("5".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("9".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200006);
                } else {
                    return new Result(CodeEnum.FAIL_200016);
                }
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1004Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1004Request 解绑 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200006.getCode(), msg);
        }
        return new Result();
    }

    @Override
    public Result saveActivationBankCard(Long userId, Integer accountType, Long id, BigDecimal money) {
        if(userId == null || id == null || money == null ||  money.doubleValue() <= 0){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(userId, accountType);
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        PayUserBalanceVO balanceVO = new PayUserBalanceVO();
        balanceVO.setUserId(userId);
        balanceVO.setUserName(dto.getUserName());
        balanceVO.setUserNo(dto.getZjMoneyNo());
        String msg = CodeEnum.FAIL_200017.getMsg();
        try {

            // //////////////////////////////////

            // 2. 实例化交易对象
            TrdT1032Request trdRequest = new TrdT1032Request();
            trdRequest.setSrl_ptnsrl(id.toString()); /** 合作方交易流水号 */
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));/** 交易日期 */

            trdRequest.setCltacc_subno(dto.getZjMoneyNo());/** 资金账号 */
            trdRequest.setCltacc_cltnm(dto.getZjAccountName()); /** 户名 */
            /**
             * 原交易类型：<br> (1:绑定、2： 变更、3：删除
             * T1031：开户绑卡<br>
             * T1034：修改绑定账户<br>
             * T1004：绑定/变更结算账户
             */
            trdRequest.setPbusitype("T1004");
            /**
             * 激活方式： <br>
             * 激活方式 1 企业主动打款激活; 3 短信验证激活; 4 企业被动打款激活
             */
            trdRequest.setActiflag("4");
            /**
             * 激活内容： <br>
             * ActiFlag=3时填写短信验证码; <br>
             * ActiFlag=4时填写来账金额
             */
            trdRequest.setActiinfo(BigDecimalUtils.multiply(money, 100).longValue() + "");

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1032Response trdResponse = new TrdT1032Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");
            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                System.out.println("[state]=[" + trdResponse.getState() + "]"); // 激活结果：1激活/验证成功;2验证成功，需人工审核激活;9激活/验证失败
                System.out.println("[amount]=[" + trdResponse.getAmount() + "]"); // 打款认证金额(ActiFlag=1时返回)
                System.out.println("[actideadline]=[" + trdResponse.getActideadline() + "]"); // 打款激活最后日期(ActiFlag=1时返回)
                //添加日志
                logDTO.setId(ymlConfig.getPkId());
                httpLogService.insert(logDTO);
                if("1".equals(trdResponse.getState())){
                    return new Result();
                } else if("2".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200012);
                } else if("9".equals(trdResponse.getState())){
                    return new Result(CodeEnum.FAIL_200006);
                } else {
                    return new Result(CodeEnum.FAIL_200017);
                }
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1032Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT1032Request 激活卡片 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200017.getCode(), msg);
        }
        return new Result(balanceVO);
    }

    @Override
    public PayUserDTO getPayUserDTO(Long userId, Integer accountType) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_id", userId);
        wrapper.eq("account_type", accountType);
        return this.selectOne(wrapper);
    }

    @Override
    public PayUserDTO getPayUserDTO(Long userId) {
        if(userId == null || userId == 0){
            return getPayUserDTO(ztgfIncomeUserId, 3);
        } else {
            return getPayUserDTO(userId, 1);
        }
    }

    @Override
    public PayUserDTO getPayUserDTO(String zjMoneyNo) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("zj_money_no", zjMoneyNo);
        return this.selectOne(wrapper);
    }

    @Override
    public Result updateNoWithdrawMoney(PayUserDTO dto) {
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        int revision = dto.getRevision();
        dto.setRevision(revision + 1);
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id", dto.getId());
        wrapper.eq("revision", revision);
        if(this.update(dto, wrapper)){
            return new Result();
        } else {
            return new Result(CodeEnum.FAIL_100008);
        }
    }

    @Override
    public Result saveNoWithdrawMoney(Long userId, Integer accountType, BigDecimal money) {
        if(userId == null || accountType == null || money == null || money.compareTo(BigDecimal.ZERO) < 0){
            return new Result(CodeEnum.FAIL_100002);
        }

        PayUserDTO dto = this.getPayUserDTO(userId, accountType);
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        dto.setNoWithdrawMoney(money);
        dto.setNoWithdrawMoneySetTime(new Date());
        dto.setNoWithdrawMoneyInit(money);

        if(this.updateNoWithdrawMoney(dto).success()){
            return new Result();
        } else {
            return new Result(CodeEnum.FAIL_100008);
        }
    }

    @Override
    public void fundDetailByHistory() {
        log.info("--------------定时任务：资金明细入库 ------------------");
        EntityWrapper<PayUserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account_type",3);
        wrapper.eq("user_id",ztgfIncomeUserId);
        List<PayUserDTO> list = this.selectList(wrapper);
        log.info("====================list="+list.size());
        if (list!=null && list.size()>0) {
            for (PayUserDTO payUserDTO : list) {
                for (int i = 1; i < 12; i++) {
                    PayZjBalanceLogBO payZjBalanceLogBO = new PayZjBalanceLogBO();
                    payZjBalanceLogBO.setUserId(payUserDTO.getUserId());
                    payZjBalanceLogBO.setAccountType(payUserDTO.getAccountType());
                    payZjBalanceLogBO.setPageNo(1);
                    payZjBalanceLogBO.setPageSize(20);

                    LocalDate endDate = LocalDate.now();
                    LocalDate biginDate = endDate.plusMonths(-i);
                    endDate=endDate.plusMonths(-(i-1)).plusDays(-1);
                    ZoneId zone = ZoneId.systemDefault();
                    Instant instant = biginDate.atStartOfDay().atZone(zone).toInstant();
                    Instant instant1 = endDate.atStartOfDay().atZone(zone).toInstant();
                    java.util.Date bdate = Date.from(instant);
                    java.util.Date edate = Date.from(instant1);

                    payZjBalanceLogBO.setStartDate(bdate);
                    payZjBalanceLogBO.setEndDate(edate);

                    Result<Page<PayZjBalanceLogVO>> pageResult = this.queryPageZjHistoryBalanceLog(payZjBalanceLogBO);
                    if (pageResult != null) {
                        Page<PayZjBalanceLogVO> data = pageResult.getData();
                        if (data != null) {
                            if (data.getTotal() > 0) {

                                int ceil= data.getTotal()%data.getSize()>0? (data.getTotal()/data.getSize()+1):data.getTotal()/data.getSize();
                                for (int j = 1; j <= ceil; j++) {
                                    payZjBalanceLogBO.setPageNo(j);
                                    Result<Page<PayZjBalanceLogVO>> pageResult1 = this.queryPageZjHistoryBalanceLog(payZjBalanceLogBO);
                                    if (pageResult1 != null) {
                                        if (pageResult1.getData() != null) {
                                            if (pageResult1.getData().getRecords() != null && pageResult1.getData().getSize() > 0) {
                                                for (PayZjBalanceLogVO record : pageResult1.getData().getRecords()) {
                                                    PayFundDetailsDTO payFundDetailsDTO = new PayFundDetailsDTO();
                                                    payFundDetailsDTO.setId(ymlConfig.getPkId());
                                                    try {
                                                        org.apache.commons.beanutils.BeanUtils.copyProperties(payFundDetailsDTO, record);
                                                    } catch (IllegalAccessException e) {
                                                        e.printStackTrace();
                                                    } catch (InvocationTargetException e) {
                                                        e.printStackTrace();
                                                    }
                                                    payFundDetailsService.insert(payFundDetailsDTO);
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                        }
                    }
                }
            }
        }
    }

    @Override
    public Result<Page<PayFundDetailsDTO>> queryPageZjHistoryBalanceLogList(PayZjBalanceBO bo) {
        //查询出所有用户
        EntityWrapper<PayUserDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account_type",1);
        List<PayUserDTO> list = this.selectList(wrapper);
        //admin账号
        EntityWrapper<PayUserDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("account_type",3);
        wrapper1.eq("user_id",ztgfIncomeUserId);
        PayUserDTO payUserDTO = this.selectOne(wrapper1);
        if (payUserDTO!=null){
            list.add(payUserDTO);
        }
        Page<PayFundDetailsDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<PayFundDetailsDTO> list1=new ArrayList<>();
        if (list!=null && list.size()>0){
            for (PayUserDTO userDTO : list) {
                bo.setUserId(userDTO.getUserId());
                Wrapper<PayFundDetailsDTO> wrapper2 = new EntityWrapper<>();
                wrapper2.eq("user_id",bo.getUserId());
                if(bo.getStartDate() != null){
                    wrapper2.ge("accounting_date",DateUtils.dateToStr(bo.getStartDate(),DateUtils.DATE_FORMAT_DATESHORTDAY));
                }
                if(bo.getEndDate() != null){
                    wrapper2.le("accounting_date",DateUtils.dateToStr(bo.getEndDate(),DateUtils.DATE_FORMAT_DATESHORTDAY));
                }
                if(StringUtils.isNotBlank(bo.getBusinessType())){
                    wrapper2.eq("business_type",bo.getBusinessType());
                }
                if(StringUtils.isNotBlank(bo.getName())){
                    wrapper2.andNew().like("user_name",bo.getName());
                }
                if(StringUtils.isNotBlank(bo.getKeyWord())){
                    wrapper2.andNew().like("money_usage",bo.getKeyWord());
                }
                List<PayFundDetailsDTO> payFundDetailsDTO=  payFundDetailsMapper.selectList(wrapper2);
                //List<PayFundDetailsDTO> payFundDetailsDTO=  payFundDetailsMapper.selectZjList(bo);
                if (payFundDetailsDTO!=null){
                    list1.addAll(payFundDetailsDTO);
                }
            }
        }
        //排序
        List<PayFundDetailsDTO> dtoList =   list1.stream().sorted(Comparator.comparing((PayFundDetailsDTO p) -> -(Integer.valueOf(p.getAccountingDate()))).reversed().thenComparing(PayFundDetailsDTO::getAccountingTime).reversed()).collect(Collectors.toList());
      //  List<PayFundDetailsDTO> dtoList = list1.stream().sorted(Comparator.comparing(PayFundDetailsDTO p) -> -p.getA.reversed().thenComparing(PayFundDetailsDTO::getAccountingTime).reversed()).collect(Collectors.toList());
        //分页
        int count=0;
        if(dtoList != null && dtoList.size() > 0) {
            page.setTotal(dtoList.size());
            count = dtoList.size();
            int fromIndex = (bo.getPageNo()-1) * bo.getPageSize();
            int toIndex = bo.getPageNo() * bo.getPageSize();
            if (toIndex > count) {
                toIndex = count;
            }
            List<PayFundDetailsDTO> payFundDetailsDTOList = dtoList.subList(fromIndex, toIndex);
            page.setRecords(payFundDetailsDTOList);
        }
        return new Result<>(page);
    }

//    @Override
//    public Result<Page<PayZjBalanceLogVO>> queryPageZjBalanceLogList(PayZjBalanceBO bo) {
//        //查询出所有用户
//        EntityWrapper<PayUserDTO> wrapper = new EntityWrapper<>();
//        wrapper.eq("account_type",1);
//        List<PayUserDTO> list = this.selectList(wrapper);
//        //admin账号
//        EntityWrapper<PayUserDTO> wrapper1 = new EntityWrapper<>();
//        wrapper1.eq("account_type",3);
//        wrapper1.eq("user_id",ztgfIncomeUserId);
//        PayUserDTO payUserDTO = this.selectOne(wrapper1);
//        if (payUserDTO!=null){
//            list.add(payUserDTO);
//        }
//        Page<PayZjBalanceLogVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
//        List<PayZjBalanceLogVO> list1=new ArrayList<>();
//        if (list!=null && list.size()>0){
//            for (PayUserDTO userDTO : list) {
//                PayZjBalanceLogBO payZjBalanceLogBO = new PayZjBalanceLogBO();
//                payZjBalanceLogBO.setUserId(userDTO.getUserId());
//                payZjBalanceLogBO.setAccountType(userDTO.getAccountType());
//                Result<List<PayZjBalanceLogVO>> listResult = this.queryPageZjBalanceLogByAdmin(payZjBalanceLogBO);
//                if (listResult.getData()!=null){
//                    list1.addAll(listResult.getData());
//                }
//            }
//        }
//        List<PayZjBalanceLogVO> rUseres=null;
//        if (StrUtils.isEmpty(bo.getName()) && StrUtils.isEmpty(bo.getKeyWord()) && StrUtils.isEmpty(bo.getBusinessType())){
//            rUseres=list1;
//        } else {
//            if (StrUtils.isEmpty(bo.getBusinessType())){
//                rUseres = list1.stream().filter(userz -> userz.getUserName().contains(bo.getName()) && userz.getMoneyUsage().contains(bo.getKeyWord())).collect(Collectors.toList());
//            }else {
//                rUseres = list1.stream().filter(userz -> userz.getBusinessType().equals(bo.getBusinessType()) && userz.getUserName().contains(bo.getName()) && userz.getMoneyUsage().contains(bo.getKeyWord())).collect(Collectors.toList());
//            }
//        }
//        rUseres.stream().sorted(Comparator.comparing((PayZjBalanceLogVO p) -> -(Integer.valueOf(p.getAccountingDate()))).reversed().thenComparing(PayZjBalanceLogVO::getAccountingTime).reversed()).collect(Collectors.toList());
//        //分页
//        int count=0;
//        if(rUseres != null && rUseres.size() > 0) {
//            page.setTotal(rUseres.size());
//            count = rUseres.size();
//            int fromIndex = (bo.getPageNo()-1) * bo.getPageSize();
//            int toIndex = bo.getPageNo() * bo.getPageSize();
//            if (toIndex > count) {
//                toIndex = count;
//            }
//            List<PayZjBalanceLogVO> logVOList = rUseres.subList(fromIndex, toIndex);
//            page.setRecords(logVOList);
//        }
//        return new Result<>(page);
//    }

    @Override
    public List<PayBalanceExpotVO> batchExportBalanceList(PayZjBalanceBO bo) {
        List<PayBalanceExpotVO> expotVOList = new ArrayList<>();
        if (StrUtils.isNotEmpty(bo.getIds())){
            String[] split = bo.getIds().split(",");
            for (String id : split) {
                PayBalanceExpotVO payBalanceExpotVO = new PayBalanceExpotVO();
                PayFundDetailsDTO payFundDetailsDTO = payFundDetailsMapper.selectById(id);
                if (payFundDetailsDTO!=null){
                    String date=payFundDetailsDTO.getAccountingDate()+payFundDetailsDTO.getAccountingTime();
                    Date date1 = DateUtils.strToDate(date, DateUtils.DATE_FORMAT_DATETIMEMS);
                    Date date2 = DateUtils.dateFormat(date1, DateUtils.DATE_FORMAT_DATETIME);
                    String s = DateUtils.dateToStr(date2);
                    payBalanceExpotVO.setBalanceDate(s);
                    if (payFundDetailsDTO.getBusinessType().equals("INMNY")){
                        payBalanceExpotVO.setBusinessType("入金");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("OTMNY")){
                        payBalanceExpotVO.setBusinessType("出金");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("FRZE")){
                        payBalanceExpotVO.setBusinessType("冻结");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("UNFRZE")){
                        payBalanceExpotVO.setBusinessType("解冻");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("PAYMNY")){
                        payBalanceExpotVO.setBusinessType("付款");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("RCVMNY")){
                        payBalanceExpotVO.setBusinessType("收款");
                    }
                    payBalanceExpotVO.setFeeMoney(payFundDetailsDTO.getFeeMoney());
                    payBalanceExpotVO.setMoeny(payFundDetailsDTO.getMoeny());
                    payBalanceExpotVO.setMoneyUsage(payFundDetailsDTO.getMoneyUsage());
                    payBalanceExpotVO.setOriginalBusinessId(payFundDetailsDTO.getOriginalBusinessId());
                    payBalanceExpotVO.setOriginalZjBusinessId(payFundDetailsDTO.getOriginalZjBusinessId());
                    payBalanceExpotVO.setUseMoney(payFundDetailsDTO.getUseMoney());
                    payBalanceExpotVO.setUserName(payFundDetailsDTO.getUserName());
                    payBalanceExpotVO.setUserNo(payFundDetailsDTO.getUserNo());
                    payBalanceExpotVO.setOtherMessage(payFundDetailsDTO.getOtherUserNo()+payFundDetailsDTO.getOtherUserName());
                    expotVOList.add(payBalanceExpotVO);
                }
            }
        }else {
            //查询出所有用户
            EntityWrapper<PayUserDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("account_type",1);
            List<PayUserDTO> list = this.selectList(wrapper);
            //admin账号
            EntityWrapper<PayUserDTO> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("account_type",3);
            wrapper1.eq("user_id",ztgfIncomeUserId);
            PayUserDTO payUserDTO = this.selectOne(wrapper1);
            if (payUserDTO!=null){
                list.add(payUserDTO);
            }
            List<PayFundDetailsDTO> list1=new ArrayList<>();
            if (list!=null && list.size()>0){
                for (PayUserDTO userDTO : list) {
                    bo.setUserId(userDTO.getUserId());
                    List<PayFundDetailsDTO> payFundDetailsDTO=  payFundDetailsMapper.selectZjList(bo);
                    if (payFundDetailsDTO!=null){
                        list1.addAll(payFundDetailsDTO);
                    }
                }
            }
            if (list1!=null && list1.size()>0){
                for (PayFundDetailsDTO payFundDetailsDTO : list1) {
                    PayBalanceExpotVO payBalanceExpotVO = new PayBalanceExpotVO();
                    String date=payFundDetailsDTO.getAccountingDate()+payFundDetailsDTO.getAccountingTime();
                    Date date1 = DateUtils.strToDate(date, DateUtils.DATE_FORMAT_DATETIMEMS);
                    Date date2 = DateUtils.dateFormat(date1, DateUtils.DATE_FORMAT_DATETIME);
                    String s = DateUtils.dateToStr(date2);
                    payBalanceExpotVO.setBalanceDate(s);
                    if (payFundDetailsDTO.getBusinessType().equals("INMNY")){
                        payBalanceExpotVO.setBusinessType("入金");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("OTMNY")){
                        payBalanceExpotVO.setBusinessType("出金");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("FRZE")){
                        payBalanceExpotVO.setBusinessType("冻结");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("UNFRZE")){
                        payBalanceExpotVO.setBusinessType("解冻");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("PAYMNY")){
                        payBalanceExpotVO.setBusinessType("付款");
                    }
                    if (payFundDetailsDTO.getBusinessType().equals("RCVMNY")){
                        payBalanceExpotVO.setBusinessType("收款");
                    }
                    payBalanceExpotVO.setFeeMoney(payFundDetailsDTO.getFeeMoney());
                    payBalanceExpotVO.setMoeny(payFundDetailsDTO.getMoeny());
                    payBalanceExpotVO.setMoneyUsage(payFundDetailsDTO.getMoneyUsage());
                    payBalanceExpotVO.setOriginalBusinessId(payFundDetailsDTO.getOriginalBusinessId());
                    payBalanceExpotVO.setOriginalZjBusinessId(payFundDetailsDTO.getOriginalZjBusinessId());
                    payBalanceExpotVO.setUseMoney(payFundDetailsDTO.getUseMoney());
                    payBalanceExpotVO.setUserName(payFundDetailsDTO.getUserName());
                    payBalanceExpotVO.setUserNo(payFundDetailsDTO.getUserNo());
                    payBalanceExpotVO.setOtherMessage(payFundDetailsDTO.getOtherUserNo()+"  "+payFundDetailsDTO.getOtherUserName());
                    expotVOList.add(payBalanceExpotVO);
                }
            }
        }
        //排序
        List<PayBalanceExpotVO> collect = expotVOList.stream().sorted(Comparator.comparing(PayBalanceExpotVO::getBalanceDate).reversed()).collect(Collectors.toList());
        log.info("expotVOList="+expotVOList.size());
        //  List<PayFundDetailsDTO> dtoList = list1.stream().sorted(Comparator.comparing(PayFundDetailsDTO p) -> -p.getA.reversed().thenComparing(PayFundDetailsDTO::getAccountingTime).reversed()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<PayBalanceExpotPcVO> batchExportBalanceListByPc(PayZjBalanceBO bo) {
        List<PayBalanceExpotPcVO> expotVOList = new ArrayList<>();
        if (StrUtils.isNotEmpty(bo.getIds())){
            String[] split = bo.getIds().split(",");
            for (String id : split) {
                PayBalanceExpotPcVO payBalanceExpotVO = new PayBalanceExpotPcVO();
                EntityWrapper<PayFundDetailsDTO> payFundDetailsDTOEntityWrapper = new EntityWrapper<>();
                payFundDetailsDTOEntityWrapper.eq("data_no",id);
                List<PayFundDetailsDTO> list = payFundDetailsMapper.selectList(payFundDetailsDTOEntityWrapper);
                if (list!=null && list.size()>0){
                    if (list.get(0)!=null){
                        PayFundDetailsDTO payFundDetailsDTO=list.get(0);
                        String date=payFundDetailsDTO.getAccountingDate()+payFundDetailsDTO.getAccountingTime();
                        Date date1 = DateUtils.strToDate(date, DateUtils.DATE_FORMAT_DATETIMEMS);
                        Date date2 = DateUtils.dateFormat(date1, DateUtils.DATE_FORMAT_DATETIME);
                        String s = DateUtils.dateToStr(date2);
                        payBalanceExpotVO.setBalanceDate(s);
                        if (payFundDetailsDTO.getBusinessType().equals("INMNY")){
                            payBalanceExpotVO.setBusinessType("入金");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("OTMNY")){
                            payBalanceExpotVO.setBusinessType("出金");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("FRZE")){
                            payBalanceExpotVO.setBusinessType("冻结");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("UNFRZE")){
                            payBalanceExpotVO.setBusinessType("解冻");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("PAYMNY")){
                            payBalanceExpotVO.setBusinessType("付款");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("RCVMNY")){
                            payBalanceExpotVO.setBusinessType("收款");
                        }
                        payBalanceExpotVO.setMoeny(payFundDetailsDTO.getMoeny());
                        payBalanceExpotVO.setMoneyUsage(payFundDetailsDTO.getMoneyUsage());
                        payBalanceExpotVO.setOtherMessage(payFundDetailsDTO.getOtherUserNo()+payFundDetailsDTO.getOtherUserName());
                        expotVOList.add(payBalanceExpotVO);
                    }
                }

            }
        }else {
            EntityWrapper<PayUserDTO> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("account_type",1);
            wrapper1.eq("user_id",bo.getUserId());
            PayUserDTO payUserDTO = this.selectOne(wrapper1);
            if (payUserDTO!=null){
                bo.setUserId(payUserDTO.getUserId());
                List<PayFundDetailsDTO> list1=  payFundDetailsMapper.selectZjList(bo);
                if (list1!=null && list1.size()>0){
                    for (PayFundDetailsDTO payFundDetailsDTO : list1) {
                        PayBalanceExpotPcVO payBalanceExpotVO = new PayBalanceExpotPcVO();
                        String date=payFundDetailsDTO.getAccountingDate()+payFundDetailsDTO.getAccountingTime();
                        Date date1 = DateUtils.strToDate(date, DateUtils.DATE_FORMAT_DATETIMEMS);
                        Date date2 = DateUtils.dateFormat(date1, DateUtils.DATE_FORMAT_DATETIME);
                        String s = DateUtils.dateToStr(date2);
                        payBalanceExpotVO.setBalanceDate(s);
                        if (payFundDetailsDTO.getBusinessType().equals("INMNY")){
                            payBalanceExpotVO.setBusinessType("入金");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("OTMNY")){
                            payBalanceExpotVO.setBusinessType("出金");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("FRZE")){
                            payBalanceExpotVO.setBusinessType("冻结");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("UNFRZE")){
                            payBalanceExpotVO.setBusinessType("解冻");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("PAYMNY")){
                            payBalanceExpotVO.setBusinessType("付款");
                        }
                        if (payFundDetailsDTO.getBusinessType().equals("RCVMNY")){
                            payBalanceExpotVO.setBusinessType("收款");
                        }
                        payBalanceExpotVO.setMoeny(payFundDetailsDTO.getMoeny());
                        payBalanceExpotVO.setMoneyUsage(payFundDetailsDTO.getMoneyUsage());
                        payBalanceExpotVO.setOtherMessage(payFundDetailsDTO.getOtherUserNo()+"  "+payFundDetailsDTO.getOtherUserName());
                        expotVOList.add(payBalanceExpotVO);
                    }
                }
            }
        }
        //排序
        List<PayBalanceExpotPcVO> collect = expotVOList.stream().sorted(Comparator.comparing(PayBalanceExpotPcVO::getBalanceDate).reversed()).collect(Collectors.toList());
        log.info("expotVOList="+expotVOList.size());
        //  List<PayFundDetailsDTO> dtoList = list1.stream().sorted(Comparator.comparing(PayFundDetailsDTO p) -> -p.getA.reversed().thenComparing(PayFundDetailsDTO::getAccountingTime).reversed()).collect(Collectors.toList());
        return collect;
    }

    //admin查询当天所有数据
    public Result<List<PayZjBalanceLogVO>> queryPageZjBalanceLogByAdmin(PayZjBalanceLogBO bo) {
        if(bo.getUserId() == null){
            return new Result(CodeEnum.FAIL_100002);
        }
        PayUserDTO dto = getPayUserDTO(bo.getUserId(), bo.getAccountType());
        if(dto == null){
            return new Result(CodeEnum.FAIL_200003);
        }

        //调用余额接口
        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;

        String msg = CodeEnum.FAIL_200011.getMsg();
        try {

            // 2. 实例化交易对象
            TrdT9007Request trdRequest = new TrdT9007Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
    //            trdRequest.setQuystdt(DateUtils.dateToStr(bo.getStartDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));     /** 起始日期YYYYMMDD */
    //            trdRequest.setQuyenddt(DateUtils.dateToStr(bo.getStartDate(), DateUtils.DATE_FORMAT_DATESHORTDAY));  /** 结束日期YYYYMMDD */
            trdRequest.setSubno(dto.getZjMoneyNo());       /** 资金账号 */
            trdRequest.setTye(bo.getType());           /** 类型(C:增加、T:减少) */
            trdRequest.setBsitye(bo.getBusinessType());/** 业务类型: INMNY入金; OTMNY出金; FRZE冻结; UNFRZE解冻; PAYMNY付款; RCVMNY收款 支持同时传入多个值，使用半角英文逗号隔开*/
            trdRequest.setDtrdtype(bo.getOriginalBusinessType());/*原交易-类型: TNSTRD出入金业务 PAYTRD支付业务 FRETRD冻结解冻业务 SHFKTRD收款业务,支持同时传入多个值，使用半角英文逗号隔开 */
            trdRequest.setPagsiz(bo.getPageSize()); /** 分页查询：每页N条数据(10<=N<=200) */
            trdRequest.setCurpag(bo.getPageNo()); /** 分页查询：当前页数 */
            trdRequest.setSortr(bo.getSort().toString());/** 排序规则:10明细编号-正序（默认）;11明细编号-倒序 */

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT9007Response trdResponse = new TrdT9007Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");

            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            List<PayZjBalanceLogVO> list = new ArrayList<>();
            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                httpSuccess = true;
                List<QuyDaT9007> quyDatList = trdResponse.getQuyDatList();
                if (null != quyDatList && !quyDatList.isEmpty()) {
                    for (QuyDaT9007 quyDat : quyDatList) {
                        PayZjBalanceLogVO logVO = new PayZjBalanceLogVO();
                        logVO.setUserId(dto.getUserId());//
                        logVO.setUserName(dto.getUserName());//
                        logVO.setUserNo(dto.getZjMoneyNo());//

                        logVO.setAccountingDate(quyDat.getDte());//记账日期 YYYYMMDD
                        logVO.setAccountingTime(quyDat.getTme());//记账时间 HH24MISS
                        logVO.setDataNo(quyDat.getMnychgno());//明细编号 默认按照此字段正序排序
                        logVO.setType(quyDat.getTye());//类型(C:增加、T:减少)
                        logVO.setOtherUserNo(quyDat.getRsubno());//对方-账号
                        logVO.setOtherUserName(quyDat.getRcltnm());//对方-户名
                        logVO.setMoeny(BigDecimalUtils.divide(quyDat.getAclamt(), 100));//发生额
                        logVO.setFeeMoney(BigDecimalUtils.divide(quyDat.getFeeamt(), 100));//手续费
                        logVO.setUseMoney(BigDecimalUtils.divide(quyDat.getBalamt(), 100));//资金变动后的可用资金余额
                        logVO.setBusinessType(quyDat.getBsitye());//业务类型 INMNY 入金; OTMNY 出金; FRZE 冻结; UNFRZE 解冻; PAYMNY 付款; RCVMNY 收款
                        logVO.setBusinessNotes(quyDat.getBsidsc());//业务描述
                        logVO.setOriginalBusinessType(quyDat.getDtrdtype());//原交易-类型 TNSTRD 出入金业务 PAYTRD 支付业务 FRETRD 冻结解冻业务 SHFKTRD 收款业务
                        logVO.setOriginalBusinessId(quyDat.getDptnsrl());//原交易-合作方交易流水号
                        logVO.setOriginalZjBusinessId(quyDat.getDplatsrl());//原交易-平台交易流水号
                        logVO.setMoneyUsage(quyDat.getUsage());//资金用途(附言)
                        list.add(logVO);
                    }
                }
            } else {
                msg = trdResponse.getMsghd_rspmsg();
            }
            return new Result(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT9008Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("TrdT9008Request 获取交易流水 500");
        }

        //添加日志
        logDTO.setId(ymlConfig.getPkId());
    //        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200011.getCode(), msg);
        }
        return new Result(null);
    }
}
